/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import beans.AuthBean;
import beans.UserFacade;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * REST Web Service
 *
 * @author tae
 */
@Path("AuthServlet")
@Stateless
public class AuthResource {

    @Context
    private UriInfo context;
    /*
    @Context
    private HttpServletRequest servletRequest;
    */
    @EJB
    private AuthBean authBean;

    /**
     * Creates a new instance of AuthResource
     */
    public AuthResource() {
    }
    

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String execute(String request, @Context HttpServletRequest servletRequest) {
        System.out.println("auth resource executed");
        JSONObject json = new JSONObject(request);
        
        long facebookId = json.getLong("facebookId");
        String gcmKey = json.getString("gcmKey");
        
        boolean result = authBean.auth(facebookId, gcmKey);
        
        if(result)
        {
            HttpSession session = servletRequest.getSession(true);
            
            session.setAttribute("loginFlag", true);
            return "SUCCESS";
        }
        else
        {
            return "FAIL";
        }
    }
}
